package com.app.Timer;

import com.app.model.cache.RedisKeys;
import com.app.model.main.Article;
import com.app.model.main.UserInfo;
import com.app.service.SolrService;
import com.app.support.utils.RedisUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Created by Lichaojie on 2017/3/18. It's a beautiful day.
 */
@Component
public class SolrTimer {

    @Autowired
    private SolrService solrService;
    @Autowired
    private RedisUtil redisUtil;

    /**
     * 将缓存中的用户信息加入到搜索服务器
     */
    @Scheduled(fixedRate = 5000)
    public void submitUser(){

        //log.info("add user-info to Solr Server !");

        if(!redisUtil.isListEmpty(RedisKeys.USERINFOTOSOLR)){
            while (!redisUtil.isListEmpty(RedisKeys.USERINFOTOSOLR)){
                UserInfo userInfo = redisUtil.getList(RedisKeys.USERINFOTOSOLR);
                solrService.addField(userInfo);
            }
        }

    }

    /**
     * 将缓存中的帖子信息加入到搜索服务器
     */
    @Scheduled(fixedRate = 5000)
    public void submitArticle(){

        //log.info("add article-info to Solr Server !");

        if(!redisUtil.isListEmpty(RedisKeys.ARTICLETOSOLR)){
            while (!redisUtil.isListEmpty(RedisKeys.ARTICLETOSOLR)){
                Article article = redisUtil.getList(RedisKeys.ARTICLETOSOLR);
                solrService.addField(article);
            }
        }

    }

}
